package com.fjh.springboot_01.service.impl;

import com.fjh.springboot_01.mapper.BannerMapper;
import com.fjh.springboot_01.pojo.Banner;
import com.fjh.springboot_01.service.BannerService;
import com.fjh.springboot_01.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class BannerServiceImpl implements BannerService {
    
    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public List<Banner> getActiveBanners() {
        return bannerMapper.selectActiveBanners();
    }

    @Override
    public List<Banner> getAllBanners() {
        return bannerMapper.selectAllBanners();
    }

    @Override
    public Banner addBanner(Banner banner, MultipartFile imageFile) {
        try {
            if (imageFile != null && !imageFile.isEmpty()) {
                String originalFilename = imageFile.getOriginalFilename();
                String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
                String newFileName = UUID.randomUUID().toString() + extension;
                String url = AliOssUtil.uploadFile(newFileName, imageFile.getInputStream());
                banner.setImageUrl(url);
            }
            
            banner.setCreateTime(LocalDateTime.now());
            banner.setUpdateTime(LocalDateTime.now());
            bannerMapper.insert(banner);
            return banner;
        } catch (Exception e) {
            throw new RuntimeException("添加轮播图失败", e);
        }
    }

    @Override
    public Banner updateBanner(Banner banner, MultipartFile imageFile) {
        try {
            // 获取原有的 Banner 信息
            Banner existingBanner = bannerMapper.selectById(banner.getId());
            if (existingBanner == null) {
                throw new RuntimeException("轮播图不存在");
            }

            // 如果有新图片上传，则更新图片URL
            if (imageFile != null && !imageFile.isEmpty()) {
                String originalFilename = imageFile.getOriginalFilename();
                String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
                String newFileName = UUID.randomUUID().toString() + extension;
                String url = AliOssUtil.uploadFile(newFileName, imageFile.getInputStream());
                banner.setImageUrl(url);
            } else {
                // 如果没有新图片上传，保留原有的图片URL
                banner.setImageUrl(existingBanner.getImageUrl());
            }
            
            banner.setUpdateTime(LocalDateTime.now());
            bannerMapper.update(banner);
            return banner;
        } catch (Exception e) {
            throw new RuntimeException("更新轮播图失败", e);
        }
    }

    @Override
    public void deleteBanner(Integer id) {
        bannerMapper.deleteById(id);
    }

    @Override
    public Banner getBannerById(Integer id) {
        return bannerMapper.selectById(id);
    }
} 